/**
 * 
 */
package es.ubu.ceudaaj.exercise.interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFormattedTextField;

import es.ubu.ceudaaj.exercise.ud02.t04.l03.Agenda;
import es.ubu.ceudaaj.exercise.ud02.t04.l03.AgendaJAXB;
import es.ubu.ceudaaj.exercise.ud02.t04.l03.model.Contactos;

/**
 * 
 * @copirygth
 * @author Miguel
 * @version
 * 
 */
public class EditarContactoJAXB extends JFrame {
	private Agenda agenda;
	private JPanel contentPane;
	private JTextField tfNombre;
	private JTextField tfApellidos;
	private JTextField tfTelefono;
	private JTextField tfEmail;
	private JLabel lblNombre;
	private JLabel lblApellidos;
	private JLabel lblTelefono;
	private JLabel lblEmail;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JLabel lblSeleccion;
	private JButton btnVer;
	private JTextField tfNum;
	private Integer num;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EditarContactoJAXB frame = new EditarContactoJAXB();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EditarContactoJAXB() {
		agenda = AgendaJAXB.getInstance();
		setTitle("Editar Contacto :");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);

		lblSeleccion = new JLabel("Seleccione el n\u00BA de Contacto");
		GridBagConstraints gbc_lblSeleccion = new GridBagConstraints();
		gbc_lblSeleccion.insets = new Insets(0, 0, 5, 5);
		gbc_lblSeleccion.gridx = 1;
		gbc_lblSeleccion.gridy = 2;
		contentPane.add(lblSeleccion, gbc_lblSeleccion);

		btnVer = new JButton("Ver");
		btnVer.addMouseListener(new BtnVerMouseListener());
		
				tfNum = new JTextField();
				GridBagConstraints gbc_tfNum = new GridBagConstraints();
				gbc_tfNum.insets = new Insets(0, 0, 5, 5);
				gbc_tfNum.fill = GridBagConstraints.HORIZONTAL;
				gbc_tfNum.gridx = 6;
				gbc_tfNum.gridy = 2;
				contentPane.add(tfNum, gbc_tfNum);
				tfNum.setColumns(10);
		GridBagConstraints gbc_btnVer = new GridBagConstraints();
		gbc_btnVer.insets = new Insets(0, 0, 5, 5);
		gbc_btnVer.gridx = 6;
		gbc_btnVer.gridy = 3;
		contentPane.add(btnVer, gbc_btnVer);

		lblNombre = new JLabel("Nombre ");
		GridBagConstraints gbc_lblNombre = new GridBagConstraints();
		gbc_lblNombre.insets = new Insets(0, 0, 5, 5);
		gbc_lblNombre.gridx = 1;
		gbc_lblNombre.gridy = 4;
		contentPane.add(lblNombre, gbc_lblNombre);

		tfNombre = new JTextField();
		GridBagConstraints gbc_tfNombre = new GridBagConstraints();
		gbc_tfNombre.anchor = GridBagConstraints.NORTH;
		gbc_tfNombre.insets = new Insets(0, 0, 5, 5);
		gbc_tfNombre.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfNombre.gridx = 6;
		gbc_tfNombre.gridy = 4;
		contentPane.add(tfNombre, gbc_tfNombre);
		tfNombre.setColumns(10);

		lblApellidos = new JLabel("Apellidos");
		GridBagConstraints gbc_lblApellidos = new GridBagConstraints();
		gbc_lblApellidos.insets = new Insets(0, 0, 5, 5);
		gbc_lblApellidos.gridx = 1;
		gbc_lblApellidos.gridy = 5;
		contentPane.add(lblApellidos, gbc_lblApellidos);

		tfApellidos = new JTextField();
		tfApellidos.setColumns(10);
		GridBagConstraints gbc_tfApellidos = new GridBagConstraints();
		gbc_tfApellidos.anchor = GridBagConstraints.NORTH;
		gbc_tfApellidos.insets = new Insets(0, 0, 5, 5);
		gbc_tfApellidos.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfApellidos.gridx = 6;
		gbc_tfApellidos.gridy = 5;
		contentPane.add(tfApellidos, gbc_tfApellidos);

		lblTelefono = new JLabel("Telefono");
		GridBagConstraints gbc_lblTelefono = new GridBagConstraints();
		gbc_lblTelefono.insets = new Insets(0, 0, 5, 5);
		gbc_lblTelefono.gridx = 1;
		gbc_lblTelefono.gridy = 6;
		contentPane.add(lblTelefono, gbc_lblTelefono);

		tfTelefono = new JTextField();
		tfTelefono.setColumns(10);
		GridBagConstraints gbc_tfTelefono = new GridBagConstraints();
		gbc_tfTelefono.insets = new Insets(0, 0, 5, 5);
		gbc_tfTelefono.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfTelefono.gridx = 6;
		gbc_tfTelefono.gridy = 6;
		contentPane.add(tfTelefono, gbc_tfTelefono);

		lblEmail = new JLabel("Email");
		GridBagConstraints gbc_lblEmail = new GridBagConstraints();
		gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmail.gridx = 1;
		gbc_lblEmail.gridy = 7;
		contentPane.add(lblEmail, gbc_lblEmail);

		tfEmail = new JTextField();
		tfEmail.setColumns(10);
		GridBagConstraints gbc_tfEmail = new GridBagConstraints();
		gbc_tfEmail.insets = new Insets(0, 0, 5, 5);
		gbc_tfEmail.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmail.gridx = 6;
		gbc_tfEmail.gridy = 7;
		contentPane.add(tfEmail, gbc_tfEmail);

		btnAceptar = new JButton("Aceptar");
		btnAceptar.addMouseListener(new BtnAceptarMouseListener());
		GridBagConstraints gbc_btnAceptar = new GridBagConstraints();
		gbc_btnAceptar.insets = new Insets(0, 0, 5, 5);
		gbc_btnAceptar.gridx = 1;
		gbc_btnAceptar.gridy = 9;
		contentPane.add(btnAceptar, gbc_btnAceptar);

		btnCancelar = new JButton("Cancelar");
		btnCancelar.addMouseListener(new BtnCancelarMouseListener());
		GridBagConstraints gbc_btnCancelar = new GridBagConstraints();
		gbc_btnCancelar.insets = new Insets(0, 0, 5, 5);
		gbc_btnCancelar.gridx = 6;
		gbc_btnCancelar.gridy = 9;
		contentPane.add(btnCancelar, gbc_btnCancelar);
	}

	private class BtnCancelarMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			dispose();
		}
	}

	private class BtnVerMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent arg0) {
			Contactos nuevocontact = new Contactos();

			num = new Integer(tfNum.getText());

			nuevocontact = agenda.seeContact(num);

			tfNombre.setText(nuevocontact.getNombre());
			tfApellidos.setText(nuevocontact.getApellidos());
			tfTelefono.setText(nuevocontact.getTelefonoTrabajo());
			tfEmail.setText(nuevocontact.getNomCorreoElectronico());

			/*
			 * else{ JOptionPane.showMessageDialog(EditarContactoJAXB.this,
			 * "El contacto no se pudo ver"); dispose(); }
			 */
		}
	}

	private class BtnAceptarMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {

			Contactos nuevocontact = new Contactos();

			num = new Integer(tfNum.getText());

			nuevocontact.setNombre(tfNombre.getText());
			nuevocontact.setApellidos(tfApellidos.getText());

			nuevocontact.setTelefonoTrabajo(tfTelefono.getText());
			nuevocontact.setNomCorreoElectronico(tfEmail.getText());

			agenda.editContact(nuevocontact, num);

			JOptionPane.showMessageDialog(EditarContactoJAXB.this,
					"El contacto  se edito correctamente");
			dispose();
			/*
			 * } else{ JOptionPane.showMessageDialog(EditarContactoJAXB.this,
			 * "El contacto no se pudo editar"); dispose(); }
			 */

		}
	}
}
